"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.utilJavaApiParser = void 0;
// MARK: 接口结束
var class_name_1 = require("./class_name");
/** java api 解析器 */
var UtilJavaApiParser = /** @class */ (function () {
    function UtilJavaApiParser() {
        /** 一层泛型检测 */
        this.resGenericReg = /(.+)«(.+)»/;
        /** 泛型检测, 例如有列表 List<int> */
        this.resGeneric2Reg = /(.+)«(.+)«(.+)»»/;
        this.refReg = /[^\/]+$/;
        //#endregion 解析 paths
    }
    UtilJavaApiParser.prototype.isArray = function (param) {
        return false;
    };
    UtilJavaApiParser.prototype.isObject = function (param) {
        return false;
    };
    /** JAVA 类型转成 TS 类型 */
    UtilJavaApiParser.prototype.javaType2TS = function (type) {
        switch (type) {
            case 'number':
            case 'double':
            case 'integer':
            case 'int64':
            case 'int32':
            case 'long':
                return 'number';
            case 'string':
            case 'date-time':
                return 'string';
            case 'boolean':
                return 'boolean';
            case 'array':
            case 'object':
                return type;
            default:
                break;
        }
        return '';
    };
    UtilJavaApiParser.prototype.getComponent = function (json, path) {
        try {
        }
        catch (error) {
        }
    };
    //#region 解析 schema
    UtilJavaApiParser.prototype.parseClassName = function (name) {
        if (class_name_1.classNameMap[name]) {
            return class_name_1.classNameMap[name];
        }
        return name;
    };
    /**
     * ['Res', 'PageInfo', 'UserInfo'] --> Res<PageInfo<UserInfo>>
     * @param list 类名列表
     * @param ind 索引
     * @returns
     */
    UtilJavaApiParser.prototype.classNameList2VarType = function (list, ind) {
        if (ind < list.length) {
            if (ind < list.length - 1) {
                return list[ind] + '<' + this.classNameList2VarType(list, ind + 1) + '>';
            }
            return list[ind];
        }
        return;
    };
    /** 获取 $ref 类名 */
    UtilJavaApiParser.prototype.getRefName = function (ref) {
        try {
            var ret = ref.match(this.refReg);
            var className = ret[0];
            var nameList = [];
            if (this.resGeneric2Reg.test(className)) {
                var regRet = className.match(this.resGeneric2Reg);
                for (var i = 1; i < regRet.length; i++) {
                    nameList.push(this.parseClassName(regRet[i]));
                }
            }
            else if (this.resGenericReg.test(className)) {
                var regRet = className.match(this.resGenericReg);
                for (var i = 1; i < regRet.length; i++) {
                    nameList.push(this.parseClassName(regRet[i]));
                }
            }
            else {
                nameList.push(this.parseClassName(className));
            }
            return this.classNameList2VarType(nameList, 0);
        }
        catch (error) {
        }
        return '';
    };
    UtilJavaApiParser.prototype.parseSchemaProp = function (data) {
    };
    /** 解析schema 属性
     * @param data
     * @param hasTail 是否是泛型
     */
    UtilJavaApiParser.prototype.parseSchemaProps = function (data, hasTail) {
        var list = [];
        for (var key in data) {
            var item = data[key];
            var varData = {};
            varData.name = key;
            varData.description = item.description;
            //解析枚举类型
            if (item.enum && item.enum.length > 0) {
                varData.type = item.enum.map(function (e) {
                    return "'" + e + "'";
                }).join('|');
            }
            else {
                var type = this.javaType2TS(item.type);
                switch (type) {
                    case '':
                    case 'object':
                        if (item.$ref) {
                            if (hasTail) {
                                varData.type = 'T';
                            }
                            else {
                                varData.type = this.getRefName(item.$ref);
                            }
                        }
                        ;
                        break;
                    case 'array':
                        if (item.items.$ref) {
                            varData.type = this.getRefName(item.items.$ref);
                        }
                        else {
                            if (item.items.type) {
                                varData.type = this.javaType2TS(item.items.type);
                            }
                        }
                        break;
                    default:
                        varData.type = type;
                        break;
                }
            }
            list.push(varData);
        }
        return list;
    };
    /** 解析 schema */
    UtilJavaApiParser.prototype.parseSchemas = function (json, ignoreFunc) {
        var map = {};
        try {
            for (var key in json.components.schemas) {
                if (key === '响应结果')
                    continue;
                if (ignoreFunc && ignoreFunc(key)) {
                    continue;
                }
                var schemaData = json.components.schemas[key];
                var className = key;
                var tail = '';
                if (this.resGeneric2Reg.test(key)) {
                    var ret = key.match(this.resGeneric2Reg);
                    className = ret[1];
                    tail = '<T>';
                }
                else if (this.resGenericReg.test(key)) {
                    var ret = key.match(this.resGenericReg);
                    className = ret[1];
                    tail = '<T>';
                }
                var data = {};
                data.nameTail = tail;
                if (class_name_1.classNameMap[className]) {
                    data.name = class_name_1.classNameMap[className];
                    data.desc = className;
                }
                else {
                    data.name = className;
                    if (schemaData['description']) {
                        data.desc = schemaData['description'];
                    }
                    else {
                        data.desc = className;
                    }
                }
                if (!map[data.name]) {
                    map[data.name] = data;
                    data.varList = this.parseSchemaProps(schemaData.properties, !!tail);
                }
            }
        }
        catch (error) {
        }
        return map;
    };
    //#endregion 解析 schema
    //#region
    //MARK: 请求的 param
    /** 解析请求的 parameters */
    UtilJavaApiParser.prototype.parsePathParams = function (params, name) {
        var _this = this;
        var clazz = {};
        clazz.name = name + '_param';
        var list = [];
        clazz.varList = list;
        clazz.desc = name + '请求参数';
        params.forEach(function (item) {
            var data = {};
            data.name = item.name;
            data.description = item.description;
            if (item.schema.$ref) {
                console.log('param $ref', item.schema.$ref);
            }
            data.type = _this.javaType2TS(item.schema.type);
            list.push(data);
        });
        return clazz;
    };
    //MARK: 请求的 body
    /** 解析请求body 数据 */
    UtilJavaApiParser.prototype.parsePathBody = function (body) {
        try {
            var schema = body.content['application/json'].schema;
            var varData = {};
            varData.name = 'data';
            varData.description = '请求数据';
            if (schema.$ref) {
                varData.type = this.getRefName(schema.$ref);
            }
            else {
                var type = this.javaType2TS(schema.type);
                switch (type) {
                    case 'array':
                        varData.typeTail = '[]';
                        if (schema.items.$ref) {
                            varData.type = this.getRefName(schema.items.$ref);
                        }
                        else {
                            varData.type = this.javaType2TS(schema.items.type);
                        }
                        break;
                    case 'object':
                        if (schema.$ref) {
                            varData.type = this.getRefName(schema.$ref);
                        }
                        else {
                            varData.type = this.javaType2TS(schema.type);
                        }
                        break;
                    default:
                        varData.type = type;
                        break;
                }
            }
            return varData;
        }
        catch (error) {
            console.log('parse path body error', error);
        }
        return undefined;
    };
    /** 解析返回数据 */
    UtilJavaApiParser.prototype.parsePathRes = function (res) {
        var schema = res['200']['content']['*/*'].schema;
        var varData = {};
        varData.name = 'res?';
        varData.description = '返回数据';
        if (schema.$ref) {
            varData.type = this.getRefName(schema.$ref);
        }
        else {
            var type = this.javaType2TS(schema.type);
            switch (type) {
                case 'array':
                    varData.typeTail = '[]';
                    if (schema.items.$ref) {
                        varData.type = this.getRefName(schema.items.$ref);
                    }
                    else {
                        varData.type = this.javaType2TS(schema.items.type);
                    }
                    break;
                case 'object':
                    if (schema.$ref) {
                        varData.type = this.getRefName(schema.$ref);
                    }
                    else {
                        varData.type = this.javaType2TS(schema.type);
                    }
                    break;
                default:
                    varData.type = type;
                    break;
            }
        }
        if (/List\<(.+)\>/.test(varData.type)) {
            varData.type = varData.type.replace('List<', 'Array<');
        }
        else if (/Map\<(.+)\>/.test(varData.type)) {
            varData.type = varData.type.replace('Map<', 'Record<');
        }
        else if (/\bRes\b/.test(varData.type)) {
            varData.type = 'Res<any>';
        }
        else {
        }
        return varData;
    };
    /** 解析接口数据
     * @param json
     * @param removePrefixUrl 移除url 头的
     */
    UtilJavaApiParser.prototype.parsePaths = function (json, removePrefixUrl) {
        var retList = [];
        for (var key in json.paths) {
            var pathData = json.paths[key];
            for (var k in pathData) {
                var httpJson = pathData[k];
                var httpData = {};
                if (httpJson.tags) {
                    httpData.tags = httpJson.tags.join(';');
                }
                httpData.desc = httpJson.summary || '';
                var url = removePrefixUrl ? key.replace(removePrefixUrl, '') : key;
                httpData.name = url.replace(/^\//, '');
                httpData.name = httpData.name.replace(/\//g, '_');
                httpData.name = httpData.name.replace(/\{.+\}/g, '');
                httpData.name = httpData.name.replace(/\_$/g, '');
                httpData.name += '_' + k;
                httpData.varList = [{ name: 'url', type: "'".concat(url, "'"), description: httpData.desc + ' URL' }];
                httpData.varList.push({ name: 'method', type: "'".concat(k, "'"), description: '请求方法' + k });
                if (httpJson.parameters) {
                    var paramData = this.parsePathParams(httpJson.parameters, httpData.name);
                    retList.push(paramData);
                    httpData.varList.push({ name: 'params', description: '请求参数', type: paramData.name });
                }
                if (httpJson.requestBody) {
                    httpData.varList.push(this.parsePathBody(httpJson.requestBody));
                }
                if (httpJson.responses) {
                    httpData.varList.push(this.parsePathRes(httpJson.responses));
                }
                retList.push(httpData);
            }
        }
        return retList;
    };
    return UtilJavaApiParser;
}());
exports.utilJavaApiParser = new UtilJavaApiParser();
